//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "SyntheticCDO.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Experimental/Credit/Basket.h>
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instrument.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CSyntheticCDO::CSyntheticCDO (Cephei::QL::Experimental::Credit::IBasket^ basket, QL::Protection::SideEnum side, Cephei::QL::Times::ISchedule^ schedule, Double upfrontRate, Double runningRate, Cephei::QL::Times::IDayCounter^ dayCounter, QL::Times::BusinessDayConventionEnum paymentConvention, Cephei::QL::Termstructures::IYieldTermStructure^ yieldTS, Cephei::QL::IPricingEngine^ QL_Pricer) : CInstrument(CSyntheticCDO::typeid)
{
    CBasket^ _Cbasket;
    CSchedule^ _Cschedule;
    CDayCounter^ _CdayCounter;
    CYieldTermStructure^ _CyieldTS;
    try
    {
#ifdef HANDLE
        _phSyntheticCDO = NULL;
#endif
        _Cbasket = safe_cast<CBasket^> (basket);
        _Cbasket->Lock();
        boost::shared_ptr<QuantLib::Basket> _basket = static_cast<boost::shared_ptr<QuantLib::Basket>> (_Cbasket->GetShared ()); 
        QuantLib::Protection::Side _side = (QuantLib::Protection::Side)side ;
        _Cschedule = safe_cast<CSchedule^> (schedule);
        _Cschedule->Lock();
        QuantLib::Schedule& _schedule = static_cast<QuantLib::Schedule&> (_Cschedule->GetReference ()); 
        QuantLib::Rate _upfrontRate = (QuantLib::Rate)ValueHelper::Convert (upfrontRate);
        QuantLib::Rate _runningRate = (QuantLib::Rate)ValueHelper::Convert (runningRate);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::BusinessDayConvention _paymentConvention = (QuantLib::BusinessDayConvention)paymentConvention ;
        _CyieldTS = safe_cast<CYieldTermStructure^> (yieldTS);
        _CyieldTS->Lock();
        Handle<QuantLib::YieldTermStructure>& _yieldTS = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CyieldTS->GetHandle ()); 
        _ppSyntheticCDO = new boost::shared_ptr<QuantLib::SyntheticCDO> (new QuantLib::SyntheticCDO ( _basket,  _side,  _schedule,  _upfrontRate,  _runningRate,  _dayCounter,  _paymentConvention,  _yieldTS ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppSyntheticCDO)->setPricingEngine (_QL_Pricer);
        SetInstrument (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppSyntheticCDO));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cbasket != nullptr) _Cbasket->Unlock();
        if (_Cschedule != nullptr) _Cschedule->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_CyieldTS != nullptr) _CyieldTS->Unlock();
    }
}
Cephei::QL::Experimental::Credit::CSyntheticCDO::CSyntheticCDO (boost::shared_ptr<QuantLib::SyntheticCDO>& childNative, Object^ owner) : CInstrument(CSyntheticCDO::typeid)
{
#ifdef HANDLE
	_phSyntheticCDO = NULL;
#endif
	_ppSyntheticCDO = &childNative;
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppSyntheticCDO));
}
Cephei::QL::Experimental::Credit::CSyntheticCDO::CSyntheticCDO (QuantLib::SyntheticCDO& childNative, Object^ owner) : CInstrument(CSyntheticCDO::typeid)
{
#ifdef HANDLE
	_phSyntheticCDO = NULL;
#endif
	_ppSyntheticCDO = new boost::shared_ptr<QuantLib::SyntheticCDO> (&childNative);
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppSyntheticCDO));
    _SyntheticCDOOwner = owner;
    _InstrumentOwner = owner;
}

Cephei::QL::Experimental::Credit::CSyntheticCDO::CSyntheticCDO (CSyntheticCDO^ copy) : CInstrument(CSyntheticCDO::typeid)
{
#ifdef HANDLE
	_phSyntheticCDO = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSyntheticCDO = new boost::shared_ptr<QuantLib::SyntheticCDO> (copy->GetShared());
        _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppSyntheticCDO));
    }
}
Cephei::QL::Experimental::Credit::CSyntheticCDO::CSyntheticCDO (System::Type^ t) : CInstrument(CSyntheticCDO::typeid)
{
#ifdef HANDLE
	_phSyntheticCDO = NULL;
#endif
	if (!t->IsSubclassOf(CSyntheticCDO::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CSyntheticCDO::CSyntheticCDO (QuantLib::Handle<QuantLib::SyntheticCDO>& childNative, Object^ owner)  : CInstrument(CSyntheticCDO::typeid)
{
	_phSyntheticCDO = &childNative;
	_ppSyntheticCDO = &static_cast<boost::shared_ptr<QuantLib::SyntheticCDO>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppSyntheticCDO));
    _SyntheticCDOOwner = owner;
}
Cephei::QL::Experimental::Credit::CSyntheticCDO::CSyntheticCDO (QuantLib::Handle<QuantLib::SyntheticCDO> childNative)  : CInstrument(CSyntheticCDO::typeid)
{
	_phSyntheticCDO = &childNative;
	_ppSyntheticCDO = &static_cast<boost::shared_ptr<QuantLib::SyntheticCDO>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppSyntheticCDO));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CSyntheticCDO::CSyntheticCDO (QuantLib::SyntheticCDO childNative)  : CInstrument(CSyntheticCDO::typeid)
{
#ifdef HANDLE
	_phSyntheticCDO = NULL;
#endif
	_ppSyntheticCDO = new boost::shared_ptr<QuantLib::SyntheticCDO> (new QuantLib::SyntheticCDO (childNative));
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppSyntheticCDO));
}
#endif

Cephei::QL::Experimental::Credit::CSyntheticCDO::~CSyntheticCDO ()
{
    if (_ppSyntheticCDO != NULL)
    {
	    delete _ppSyntheticCDO;
        _ppSyntheticCDO = NULL;
    }
}
Cephei::QL::Experimental::Credit::CSyntheticCDO::!CSyntheticCDO ()
{
    if (_ppSyntheticCDO != NULL)
    {
	    delete _ppSyntheticCDO;
    }
}
QuantLib::SyntheticCDO& Cephei::QL::Experimental::Credit::CSyntheticCDO::GetReference ()
{
    if (_ppSyntheticCDO == NULL) throw gcnew NativeNullException ();
	return **_ppSyntheticCDO;
}
boost::shared_ptr<QuantLib::SyntheticCDO>& Cephei::QL::Experimental::Credit::CSyntheticCDO::GetShared ()
{
    if (_ppSyntheticCDO == NULL) throw gcnew NativeNullException ();
	return *_ppSyntheticCDO;
}
QuantLib::SyntheticCDO* Cephei::QL::Experimental::Credit::CSyntheticCDO::GetPointer ()
{
    if (_ppSyntheticCDO == NULL) throw gcnew NativeNullException ();
	return &**_ppSyntheticCDO;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SyntheticCDO>& Cephei::QL::Experimental::Credit::CSyntheticCDO::GetHandle ()
{
	if (_phSyntheticCDO == NULL)
	{
		_phSyntheticCDO = new Handle<QuantLib::SyntheticCDO> (*_ppSyntheticCDO);
	}
	return *_phSyntheticCDO;
}
#endif
bool Cephei::QL::Experimental::Credit::CSyntheticCDO::HasNative () 
{
	return (_ppSyntheticCDO != NULL);
}

Cephei::QL::Experimental::Credit::IBasket^ Cephei::QL::Experimental::Credit::CSyntheticCDO::Basket::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::Basket> _rv = (boost::shared_ptr<QuantLib::Basket>)(*_ppSyntheticCDO)->basket ( );   
        Cephei::QL::Experimental::Credit::CBasket^ _nrv = gcnew Cephei::QL::Experimental::Credit::CBasket (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Experimental::Credit::CSyntheticCDO::Error::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppSyntheticCDO)->error ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Experimental::Credit::CSyntheticCDO::ExpectedTrancheLoss::get ()
{
    try
    {
    	std::vector<QuantLib::Real> _rv = (std::vector<QuantLib::Real>)(*_ppSyntheticCDO)->expectedTrancheLoss ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CSyntheticCDO::FairPremium::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppSyntheticCDO)->fairPremium ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CSyntheticCDO::FairUpfrontPremium::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppSyntheticCDO)->fairUpfrontPremium ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Experimental::Credit::CSyntheticCDO::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppSyntheticCDO)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CSyntheticCDO::PremiumValue::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppSyntheticCDO)->premiumValue ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CSyntheticCDO::ProtectionValue::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppSyntheticCDO)->protectionValue ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CSyntheticCDO::RemainingNotional::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSyntheticCDO)->remainingNotional ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CSyntheticCDO::PremiumLegNPV::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSyntheticCDO)->premiumLegNPV ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Experimental::Credit::CSyntheticCDO::ProtectionLegNPV::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppSyntheticCDO)->protectionLegNPV ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::ISyntheticCDO^ Cephei::QL::Experimental::Credit::CSyntheticCDO_Factory::Create (Cephei::QL::Experimental::Credit::IBasket^ basket, QL::Protection::SideEnum side, Cephei::QL::Times::ISchedule^ schedule, Double upfrontRate, Double runningRate, Cephei::QL::Times::IDayCounter^ dayCounter, QL::Times::BusinessDayConventionEnum paymentConvention, Cephei::QL::Termstructures::IYieldTermStructure^ yieldTS, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CSyntheticCDO ( basket,  side,  schedule,  upfrontRate,  runningRate,  dayCounter,  paymentConvention,  yieldTS,  QL_Pricer);
}
